package com.esm.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.esm.model.User;
import com.esm.service.UserService;
import com.esm.serviceimpl.UserServiceImpl;
import com.esm.utils.PagingUtil;

/**
 * DepartmentSearchController
 * 根据部门名称搜索员工基本信息控制者
 * @author 魏长均
 *
 */
public class DepartmentSearchController extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	UserService uService = new UserServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
	{
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
	{
		HttpSession session = req.getSession();
		int rid = 7;
		int dep_id = 0;
		String page = "";
		String udname = "";
		//获取请求部门名称参数——做比较使用
		String dname = req.getParameter("udname");
		//获取请求域里面的部门名称
		String sdname = (String) session.getAttribute("udname");
		//如果请求域里面部门名称不为空且不等于获取的部门名称请求参数
		//——说明进行了第二次搜索:这是将page=null——方便后序分页判断;
		if(null != sdname && sdname !=dname)
		{
			page = null;
		}
		page = req.getParameter("page");
		if(page == null)
		{
			page = "1";
			//获取请求部门名称参数
			udname = req.getParameter("udname");
			//根据部门名称获取部门id号
			dep_id = uService.getDidByDname(udname);
			session.setAttribute("udname", udname);
			session.setAttribute("dep_id", dep_id);
		}
		else
		{
			//如果page != null:这说明用户点击了其他分页，那么dep_id将从前面存的session里面去获取
			dep_id = (Integer) session.getAttribute("dep_id");
		}
		List<User> employees = uService.getEmployeesByDepid(rid, dep_id,Integer.parseInt(page), 4);	
		int ucount  =uService.getDeptEmpolyeesCount(rid, dep_id);
		session.setAttribute("employees", employees);
		String  pageList = PagingUtil.getPagingList(ucount, Integer.parseInt(page), 4);
		session.setAttribute("pageList", pageList);
		req.getRequestDispatcher("path/updateemp.jsp").forward(req, resp);
	}
}
